package org.example.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 无重复字符的最长子串 : <a href="https://leetcode.cn/problems/longest-substring-without-repeating-characters/">...</a>
 *
 * @Author ZSL
 * @Date 2022/9/28 17:10
 * @Version 1.0
 */
public class Three3 {

    public static void main(String[] args) {
        String s = "au";
        int i = lengthOfLongestSubstring(s);
        System.out.println(i);
    }

    public static int lengthOfLongestSubstring(String s) {

        int success = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            int star = 0;
            if (map.containsKey(s.charAt(i))) {
                star = Math.max(star, map.get(s.charAt(i)));
            }
            map.put(s.charAt(i), i + 1);
            success = Math.max(success, i - star + 1);
        }
        return success;
    }

}
